/**
 * AUTO_COPYRIGHT_SUB_TAG
 */
package com.rim.samples.device.push.ui;

import net.rim.device.api.system.Clipboard;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.MenuItem;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

/**
 * Logger screen, contains log messages from the application
 */
public class RimLogScreen extends MainScreen {

    private VerticalFieldManager vfm;
    private Font fnt;
    private Font warnFnt;

    public RimLogScreen() {
        setTitle( "Log screen" );
        vfm = new VerticalFieldManager();
        fnt = this.getFont().derive( Font.PLAIN );
        warnFnt = fnt.derive( Font.BOLD );
        addMenuItem( new ClearLogMenuItem() );
        addMenuItem( new CopyLogMenuItem() );
        add( vfm );
    }

    public void populate( String[] messages, boolean[] warnings ) {
        vfm.deleteAll();

        int length = messages.length;
        for( int i = 0; i < length; i++ ) {
            String message = messages[ i ];
            if( message == null ) {
                break;
            }
            boolean warning = warnings[ i ];

            LabelField lf = new LabelField( message, Field.FOCUSABLE | Field.USE_ALL_WIDTH );
            lf.setFont( warning ? warnFnt : fnt );
            vfm.insert( lf, 0 );
        }
    }

    private static class ClearLogMenuItem extends MenuItem {

        public ClearLogMenuItem() {
            super( "Clear Log", 0, 1 );
        }

        public void run() {
            //RimLogger.clear();
        }
    }

    private class CopyLogMenuItem extends MenuItem {

        public CopyLogMenuItem() {
            super( "Copy Log Item", 0, 1 );
        }

        public void run() {
            String message = null;
            int fieldCount = vfm.getFieldCount();
            for( int i = 0; i < fieldCount; i++ ) {
                Field field = vfm.getField( i );
                if( field.isFocus() && field instanceof LabelField ) {
                    message = ( (LabelField) field ).getText();
                    break;
                }
            }
            if( message != null ) {
                Clipboard clipboard = Clipboard.getClipboard();
                if( clipboard != null ) {
                    clipboard.put( message );
                }
            }
        }
    }

}
